package com.tanqing.ddd.web.bean;

import java.io.Serializable;

public class PagedRequest<T> extends Request<T> implements Serializable {
    public static final int DEFAULT_PAGE_SIZE = 20;
    public static final int DEFAULT_PAGE_NUMBER = 1;
    private int pageNumber = 1;
    private int pageSize = 20;
    private String sortField;
    private String sortType;
    private boolean fetchAll = false;
    private boolean countTotal = false;

    public int getPageNumber() {
        return this.pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public PagedRequest() {
    }

    public void setPageParam(int currentPage, int pageSize, boolean fetchAll, boolean countTotal) {
        this.setCurrentPage(currentPage);
        this.setPageSize(pageSize);
        this.fetchAll = fetchAll;
        this.countTotal = countTotal;
    }

    public String getSortField() {
        return this.sortField;
    }

    public void setSortField(String sortField) {
        this.sortField = sortField;
    }

    public String getSortType() {
        return this.sortType;
    }

    public void setSortType(String sortType) {
        this.sortType = sortType;
    }

    public void setPageParam(int currentPage, int pageSize) {
        this.setPageParam(currentPage, pageSize, false, false);
    }

    public void setPageParam(int currentPage, int pageSize, boolean countTotal) {
        this.setPageParam(currentPage, pageSize, false, countTotal);
    }

    public int getCurrentPage() {
        return this.pageNumber;
    }

    public void setCurrentPage(int currentPage) {
        this.pageNumber = this.resolveCurrentPage(currentPage);
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = this.resolvePageSize(pageSize);
    }

    public boolean isFetchAll() {
        return this.fetchAll;
    }

    public void setFetchAll(boolean fetchAll) {
        this.fetchAll = fetchAll;
    }

    public boolean isCountTotal() {
        return this.countTotal;
    }

    public void setCountTotal(boolean countTotal) {
        this.countTotal = countTotal;
    }

    public int resolvePageSize(int pageSize) {
        if (pageSize <= 0) {
            pageSize = 20;
        }

        return pageSize;
    }

    public int resolveCurrentPage(int currentPage) {
        if (currentPage <= 0) {
            currentPage = 1;
        }

        return currentPage;
    }

    public int getMySQLOffset() {
        return (this.pageNumber - 1) * this.pageSize;
    }
}